package com.focusai.efairy.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.focusai.efairy.EFApplication;

/**
 * 偏好设置工具类
 */
public class PreferencesUtil {
    private static final String TAG = PreferencesUtil.class.getSimpleName();
    private static final String DEFAULT_NAME = "efairy_tmp";

    public enum PrefType {
        temp, system, service;
    }

    private SharedPreferences mTemp;
    private SharedPreferences mSystem;
    private SharedPreferences mService;//对不同帐套

    private volatile static PreferencesUtil mPreferencesUtil;

    private PreferencesUtil() {

    }

    /**
     * 单例模式产生PreferencesUtil对象
     *
     * @return
     */
    public static PreferencesUtil getInstance() {
        if (mPreferencesUtil == null) {
            synchronized (PreferencesUtil.class) {
                if (mPreferencesUtil == null) {
                    mPreferencesUtil = new PreferencesUtil();
                }
            }
        }
        mPreferencesUtil.mTemp = getTempSharedPreferences(EFApplication.getContext());
        mPreferencesUtil.mSystem = getSystemSharedPreferences(EFApplication.getContext());
        mPreferencesUtil.mService = getServiceSharedPreferences(EFApplication.getContext(), 0);

        return mPreferencesUtil;
    }


    /**
     * 用于保存临时数据
     */
    private static SharedPreferences getTempSharedPreferences(Context context) {
        return context.getSharedPreferences(DEFAULT_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 用于保存整个系统设置
     */
    private static SharedPreferences getSystemSharedPreferences(Context context) {

        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    /**
     * 用于保存对应帐套的信息
     *
     * @param context
     * @param userId  帐套id，null表示单机状态无帐套
     * @return
     */
    private static SharedPreferences getServiceSharedPreferences(Context context, long userId) {
        //考虑到切换帐套需要改变实例，这里使用动态获取实例，不做单例
        return context.getSharedPreferences("service_" + userId, Context.MODE_PRIVATE);
    }

    /**
     * 获取对应用户的sp文件
     *
     * @return
     */
    public static SharedPreferences getServiceSharedPreferences(long userId) {
        return getServiceSharedPreferences(EFApplication.getContext(), userId);
    }

    /**
     * 获取当前用户的sp文件
     *
     * @return
     */
    public static SharedPreferences getCurrentUserSharedPreferences() {
        return getServiceSharedPreferences(EFApplication.getContext(),
                PreferencesUtil.getInstance().getLong(PrefType.system, PreferenceKeys.CURRENT_LOGIN_USER_ID, 0));
    }

    /**
     * 根据类型返回sharepreference
     */
    public SharedPreferences getSharePref(PrefType type) {
        if (type == null) throw new NullPointerException("type cannot null");
        switch (type) {
            case temp:
                return mTemp;
            case system:
                return mSystem;
            case service:
                return mService;
            default:
                throw new IllegalArgumentException("shared preference type should be temp,system or service:" + type);
        }
    }

    /**
     * 保存整形
     *
     * @param key
     * @param value
     */
    public boolean putInt(PrefType type, String key, int value) {
        return getSharePref(type).edit().putInt(key, value).commit();
    }

    /**
     * 保存float类型
     *
     * @param type
     * @param key
     * @param value
     * @return
     */
    public boolean putFloat(PrefType type, String key, float value) {
        return getSharePref(type).edit().putFloat(key, value).commit();
    }

    /**
     * 保存长整形
     *
     * @param key
     * @param value
     */
    public boolean putLong(PrefType type, String key, long value) {
        return getSharePref(type).edit().putLong(key, value).commit();
    }

    /**
     * 保存字符串
     *
     * @param key
     * @param value
     */
    public boolean putString(PrefType type, String key, String value) {
        return getSharePref(type).edit().putString(key, value).commit();
    }

    /**
     * 保存字符串
     *
     * @param key
     * @param value
     */
    public void putStringApply(PrefType type, String key, String value) {
        getSharePref(type).edit().putString(key, value).apply();
    }

    /**
     * 保存布尔值
     *
     * @param key
     * @param value
     */
    public boolean putBoolean(PrefType type, String key, boolean value) {
        return getSharePref(type).edit().putBoolean(key, value).commit();
    }

    /**
     * 获取整形
     *
     * @param type
     * @param key
     * @param defValue
     * @return
     */
    public int getInt(PrefType type, String key, int defValue) {
        return getSharePref(type).getInt(key, defValue);
    }

    /**
     * 获取长整形
     *
     * @param type
     * @param key
     * @param defValue
     * @return
     */
    public long getLong(PrefType type, String key, long defValue) {
        return getSharePref(type).getLong(key, defValue);
    }

    /**
     * 获取float
     *
     * @param type
     * @param key
     * @param defValue
     * @return
     */
    public float getFloat(PrefType type, String key, float defValue) {
        return getSharePref(type).getFloat(key, defValue);
    }


    /**
     * 保存字符串
     *
     * @param type
     * @param key
     * @param defValue
     * @return
     */
    public String getString(PrefType type, String key, String defValue) {
        return getSharePref(type).getString(key, defValue);
    }

    /**
     * 获取布尔值
     *
     * @param type
     * @param key
     * @param defValue
     * @return
     */
    public boolean getBoolean(PrefType type, String key, boolean defValue) {
        return getSharePref(type).getBoolean(key, defValue);
    }

}
